package com.scuvc.controller.admin;

import com.scuvc.entity.BrandInfo;
import com.scuvc.entity.CarInfo;
import com.scuvc.entity.CategoryInfo;
import com.scuvc.entity.ModelInfo;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.JMenuBarUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;

public class AddCarView extends JFrame {
    public void addStart(){
        setTitle("新车入库");
        //设置窗体起始位置
        setLocation(400,0);
        //设置窗体大小
        setSize(570,720);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_addCar.jpg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f, getWidth(),getHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(getWidth(),getHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());
        //设置标题栏
        setTop(pan);

        JPanel panCenter = new JPanel();
        panCenter.setOpaque(false);
        GridLayout grid = new GridLayout(9,4);
        grid.setVgap(20);
        grid.setHgap(10);
        panCenter.setLayout(grid);
        Font font = new Font("仿宋",Font.BOLD,24);

        JLabel j1L1 = new JLabel();
        JLabel j1L2 = new JLabel("车牌号：");
        j1L2.setFont(font);
        j1L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j1f3 = new JTextField();
        j1f3.setFont(font);
        JLabel j1L4 = new JLabel();

        JLabel j2L1 = new JLabel();
        JLabel j2L2 = new JLabel("品\u3000牌：");
        j2L2.setFont(font);
        j2L2.setHorizontalAlignment(SwingConstants.CENTER);
        //查询获取所有品牌信息获取品牌名字的下拉框
        String[] bItems = StaticUtils.brandInfoService.getAllBrand();
        JComboBox<String> comboBoxB = new JComboBox<>(bItems);
        comboBoxB.setFont(font);
        JLabel j2L4 = new JLabel();

        JLabel j3L1 = new JLabel();
        JLabel j3L2 = new JLabel("模\u3000式：");
        j3L2.setFont(font);
        j3L2.setHorizontalAlignment(SwingConstants.CENTER);
        String[] mItems = StaticUtils.modelInfoService.getAllModel();
        JComboBox<String> comboBoxM = new JComboBox<>(mItems);
        comboBoxM.setFont(font);
        JLabel j3L4 = new JLabel();

        JLabel j4L1 = new JLabel();
        JLabel j4L2 = new JLabel("类\u3000别：");
        j4L2.setFont(font);
        j4L2.setHorizontalAlignment(SwingConstants.CENTER);
        String[] cItems = StaticUtils.categoryInfoService.getAllCategory();
        JComboBox<String> comboBoxC = new JComboBox<>(cItems);
        comboBoxC.setFont(font);
        JLabel j4L4 = new JLabel();

        JLabel j5L1 = new JLabel();
        JLabel j5L2 = new JLabel("排\u3000量：");
        j5L2.setFont(font);
        j5L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j5f3 = new JTextField();
        j5f3.setFont(font);
        JLabel j5L4 = new JLabel();

        JLabel j6L1 = new JLabel();
        JLabel j6L2 = new JLabel("市场价：");
        j6L2.setFont(font);
        j6L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j6f3 = new JTextField();
        j6f3.setFont(font);
        JLabel j6L4 = new JLabel();

        JLabel j7L1 = new JLabel();
        JLabel j7L2 = new JLabel("租\u3000金：");
        j7L2.setFont(font);
        j7L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j7f3 = new JTextField();
        j7f3.setFont(font);
        JLabel j7L4 = new JLabel();

        JLabel j8L1 = new JLabel();
        JLabel j8L2 = new JLabel("押\u3000金：");
        j8L2.setFont(font);
        j8L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j8f3 = new JTextField();
        j8f3.setFont(font);
        JLabel j8L4 = new JLabel();

        JLabel j9L1 = new JLabel();
        JButton j9b2 = new JButton("入\u3000库");
        j9b2.setFont(font);
        JButton j9b3 = new JButton("重\u3000置");
        j9b3.setFont(font);
        JButton j9b4 = new JButton("返\u3000回");
        j9b4.setFont(font);
        j9b4.setRequestFocusEnabled(false);
        panCenter.add(j1L1);
        panCenter.add(j1L2);
        panCenter.add(j1f3);
        panCenter.add(j1L4);

        panCenter.add(j2L1);
        panCenter.add(j2L2);
        panCenter.add(comboBoxB);
        panCenter.add(j2L4);

        panCenter.add(j3L1);
        panCenter.add(j3L2);
        panCenter.add(comboBoxM);
        panCenter.add(j3L4);

        panCenter.add(j4L1);
        panCenter.add(j4L2);
        panCenter.add(comboBoxC);
        panCenter.add(j4L4);

        panCenter.add(j5L1);
        panCenter.add(j5L2);
        panCenter.add(j5f3);
        panCenter.add(j5L4);

        panCenter.add(j6L1);
        panCenter.add(j6L2);
        panCenter.add(j6f3);
        panCenter.add(j6L4);

        panCenter.add(j7L1);
        panCenter.add(j7L2);
        panCenter.add(j7f3);
        panCenter.add(j7L4);

        panCenter.add(j8L1);
        panCenter.add(j8L2);
        panCenter.add(j8f3);
        panCenter.add(j8L4);

        panCenter.add(j9L1);
        panCenter.add(j9b2);
        panCenter.add(j9b3);
        panCenter.add(j9b4);

        pan.add(panCenter,BorderLayout.CENTER);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //点击事件
        j9b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 新增车辆业务
        });
        j9b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                j1f3.setText("");
                j5f3.setText("");
                j6f3.setText("");
                j7f3.setText("");
                j8f3.setText("");
            }
        });
        j9b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                j1f3.requestFocusInWindow();
                AdminView av = new AdminView();
                setVisible(false);
                av.adminShow();
            }
        });
        FocusListener focusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j1f3.setBackground(Color.BLACK);
                j1f3.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
               j1f3.setBackground(Color.WHITE);
               j1f3.setForeground(Color.BLACK);
               String text = j1f3.getText();
               if(text.equals("")){
                   JOptionPane.showMessageDialog(null,
                           "车牌号不能为空","提示",
                           JOptionPane.INFORMATION_MESSAGE);
               }
               String carNo = StaticUtils.carInfoService.getCarInfonByCarNo(text);
               if(carNo!=null){
                   JOptionPane.showMessageDialog(null,
                           "车牌号已存在","提示",
                           JOptionPane.INFORMATION_MESSAGE);
               }

            }
        };
        j1f3.addFocusListener(focusListener);
        StaticUtils.setFoucusListener(j5f3);
        StaticUtils.setFoucusListener(j6f3);
        StaticUtils.setFoucusListener(j7f3);
        StaticUtils.setFoucusListener(j8f3);
    }


    private void setTop(JPanel pan) {
        JPanel panTop = new JPanel();
        panTop.setLayout(new GridLayout(1,1));
        panTop.setOpaque(false);
        JLabel jTop = new JLabel("车辆入库");
        jTop.setPreferredSize(new Dimension(
                jTop.getPreferredSize().width,100));
        jTop.setFont(new Font("仿宋",Font.BOLD,36));
        jTop.setHorizontalAlignment(SwingConstants.CENTER);
        panTop.add(jTop);
        pan.add(panTop,BorderLayout.NORTH);
    }
}
